/**
 * ******************************************************************************************************
 * @App: dc
 * @author: alisa
 * @type: js
 *
 * @descriptions:
 *  添加机柜弹框操作
 *
 * ******************************************************************************************************
 */

import { Component, OnInit } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { ErrorStateMatcher } from '@angular/material/core';
import {FormControl, Validators, FormBuilder, FormGroup} from '@angular/forms';
import { ApisService } from "../../shared/services/requests/apis.service";
import { RequestsService } from '../../shared/services/requests/requests.service';
import { ValidatorsService } from "../../shared/services/public/validators/validators.service";
import { cabinetGroupList } from "../../shared/interface/datas/cabinetGroupList";
import { cabinetType } from "../../shared/interface/datas/cabinetType";
import {EventEmitterService} from "../../shared/services/public/event-emitter/event-emitter.service";

@Component({
  selector: 'app-add-cabinet',
  templateUrl: './add-cabinet.component.html',
  styleUrls: ['./add-cabinet.component.scss']
})
export class AddCabinetComponent implements OnInit {

  cabinetGroupList: any;

  cabinetTypeList: any;

  addCabinetForm: any;

  loading: boolean; // loading动画

  getGroupLoading: boolean; // 获取机柜组时的loading动画

  getTypeLoading: boolean; // 获取机柜类型的loading动画

  errMsg: string; // 提交失败时错误提示

  addCabinetApi = this.apis.apis.addCabinet; // api
  cabinetGroupApi = this.apis.apis.cabinetGroupList; // 获取机柜组列表的api
  cabinetTypeApi = this.apis.apis.cabinetType; // 获取机柜类型api

  constructor(
    public bsModalRef: BsModalRef,
    private apis: ApisService,
    private req: RequestsService,
    private fb: FormBuilder,
    private validatorsService: ValidatorsService,
    private eventEmitter: EventEmitterService
  ) {
    this.createForm();
  }

  // 需要注入的校验规则
  private validators = this.validatorsService.validators;  // 校验规则集
  private v_cabinetLabel = this.validators.cabinetLabel;
  private v_cabinetSpace = this.validators.cabinetSpace;
  private v_modelNumber = this.validators.modelNumber;
  private v_serialNumber = this.validators.serialNumber;
  private v_smpAddress = this.validators.SMPAddress;
  private v_cabinetWeight = this.validators.cabinetWeight;
  private v_computedPower = this.validators.computedPower;
  private v_cabinetGroupAndType = this.validators.cabinetGroupAndType;

  /**
   * *************************************************************************
   * 创建表单
   *
   */
  private createForm(){
    this.addCabinetForm = this.fb.group({
      // 机柜标签
      cabinetLabel: ['', this.v_cabinetLabel.valids()],
      // 机柜组
      cabinetGroup: ['', this.v_cabinetGroupAndType.valids()],
      // 机柜类型
      cabinetType: ['', this.v_cabinetGroupAndType.valids()],
      // 机柜型号
      modelNumber: ['', this.v_modelNumber.valids()],
      // 机柜序列号
      serialNumber: ['', this.v_serialNumber.valids()],
      // 机柜空间
      space: ['', this.v_cabinetSpace.valids()],
      // 机柜重量
      cabinetWeight: ['', this.v_cabinetWeight.valids()],
      // 机柜预承载能耗
      computedPower: ['', this.v_computedPower.valids()],
      // 机柜门禁ip地址
      doorSensorIP: ['', this.v_smpAddress.valids()]
    })
  }


  /**
   * *************************************************************************
   * 获取机柜组列表
   *
   */
  private getCabinetGroup(){
    let param = {
      name: ''
    };
    this.getGroupLoading = true;
    this.addCabinetForm.controls['cabinetGroup'].disable(); // 禁用下拉框

    /** 从localStorage获取机柜组列表 */
    this.getGroupLoading = false;
    this.addCabinetForm.controls['cabinetGroup'].enable();
    this.cabinetGroupList = JSON.parse(localStorage.getItem('dcCabinetList'));

    //this.req.postData(this.cabinetGroupApi, param)
    //  .subscribe(res=>{
    //    res as cabinetGroupList;
    //    let val = res.list;
    //    console.log("获取机柜组数据成功");
    //    console.log(res);
    //    this.getGroupLoading = true;
    //    this.addCabinetForm.controls['cabinetGroup'].enable();
    //
    //    this.cabinetGroupList = val;
    //    console.log(this.cabinetGroupList);
    //  },err=>{
    //
    //  })
  }

  /**
   * *************************************************************************
   * 获取机柜类型列表
   *
   */
  private getCabinetType(){
    let param = {
      name: ''
    };
    this.getTypeLoading = true;
    this.addCabinetForm.controls['cabinetType'].disable();
    this.req.postData(this.cabinetTypeApi, param)
      .subscribe(res=>{
        res as cabinetType;
        let val = res.list;
        console.log("获取机柜类型数据成功");
        console.log(res);
        this.getTypeLoading = false;
        this.addCabinetForm.controls['cabinetType'].enable();

        this.cabinetTypeList = val;
        console.log(this.cabinetTypeList);
      },err=>{
        this.getTypeLoading = false;
      })
  }

  /**
   * *************************************************************************
   * 提交表单
   *
   */
  public doSubmit() {

    this.loading = true;
    let params = {
      label: this.addCabinetForm.value.cabinetLabel,
      group: this.addCabinetForm.value.cabinetGroup,
      type: this.addCabinetForm.value.cabinetType,
      model: this.addCabinetForm.value.modelNumber,
      sn: this.addCabinetForm.value.serialNumber,
      space: this.addCabinetForm.value.space,
      weight: this.addCabinetForm.value.cabinetWeight,
      computedPower: this.addCabinetForm.value.computedPower,
    };
    console.log("添加机柜提交的参数");
    console.log(params);
    this.req.addData(this.addCabinetApi, params)
      .subscribe(res => {

        this.bsModalRef.hide(); // 关闭弹框
        this.loading = false;

        let self = this;
        setTimeout(function(){
          self.eventEmitter.modalAddCabinet.emit();
        }, 600);

      }, err => {
        console.log("添加机柜失败");
        console.log(params);
        this.errMsg = 'Request Failed !';
        this.loading = false;
      })

  }

  ngOnInit() {

    this.getCabinetGroup(); // 获取机柜组
    this.getCabinetType(); // 获取机柜类型

  }

}
